package com.ruoyi.web.controller.english;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.english.domain.EnBook;
import com.ruoyi.english.service.IEnBookService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 单词本Controller
 * 
 * @author ruoyi
 * @date 2021-12-25
 */
@RestController
@RequestMapping("/english/book")
public class EnBookController extends BaseController
{
    @Autowired
    private IEnBookService enBookService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询单词本列表
     */
    @PreAuthorize("@ss.hasPermi('english:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnBook enBook)
    {
        startPage();
        List<EnBook> list = enBookService.selectEnBookList(enBook);
        return getDataTable(list);
    }

    /**
     * 导出单词本列表
     */
    @PreAuthorize("@ss.hasPermi('english:book:export')")
    @Log(title = "单词本", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EnBook enBook)
    {
        List<EnBook> list = enBookService.selectEnBookList(enBook);
        ExcelUtil<EnBook> util = new ExcelUtil<EnBook>(EnBook.class);
        return util.exportExcel(list, "单词本数据");
    }

    /**
     * 获取单词本详细信息
     */
    @PreAuthorize("@ss.hasPermi('english:book:query')")
    @GetMapping(value = "/{bookId}")
    public AjaxResult getInfo(@PathVariable("bookId") Long bookId)
    {
        return AjaxResult.success(enBookService.selectEnBookByBookId(bookId));
    }

    /**
     * 新增单词本
     */
    @PreAuthorize("@ss.hasPermi('english:book:add')")
    @Log(title = "单词本", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnBook enBook, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        enBook.setCreateBy(loginUser.getUsername());
        enBook.setUserId(loginUser.getUserId());
        return toAjax(enBookService.insertEnBook(enBook));
    }

    /**
     * 修改单词本
     */
    @PreAuthorize("@ss.hasPermi('english:book:edit')")
    @Log(title = "单词本", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnBook enBook)
    {
        return toAjax(enBookService.updateEnBook(enBook));
    }

    /**
     * 删除单词本
     */
    @PreAuthorize("@ss.hasPermi('english:book:remove')")
    @Log(title = "单词本", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bookIds}")
    public AjaxResult remove(@PathVariable Long[] bookIds)
    {
        return toAjax(enBookService.deleteEnBookByBookIds(bookIds));
    }
}
